﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ObjectLibrary;

namespace _0912089_0912172
{
    public class Radar : VisibleGameEntity2D
    {
        List<Vector2> _listObjectPositions;
        Texture2D _objectTexture;
        float _radius;
        Vector2 _radarPosition;
        float _rotation;

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }
        public Vector2 RadarPosition
        {
            get { return _radarPosition; }
            set { _radarPosition = value; }
        }


        public Radar(ContentManager content,float radius)
        {
            _texture = content.Load<Texture2D>(@"Resources/InGame2DResources/Radar");
            _objectTexture = content.Load<Texture2D>(@"Resources/InGame2DResources/RadarObject");
            _listObjectPositions = new List<Vector2>();
            _radius = radius;
            _radarPosition = Vector2.Zero;
            _rotation = 0;
        }
        public void Clear()
        {
            _listObjectPositions.Clear();
        }
        public void addPosition(Vector2 pos)
        {
            Vector2 d = pos - _radarPosition;
            if (Math.Sqrt(d.X * d.X + d.Y * d.Y) < _radius)
            {
                _listObjectPositions.Add(pos);
            }
        }
        public override void Update(GameTime gameTime)
        {
            
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture,Position, Color.White);
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle screenRect)
        {

            spriteBatch.Draw(_texture, screenRect, new Rectangle(0, 0, _texture.Width, _texture.Height), Color.White);
            foreach (Vector2 pos in _listObjectPositions)
            {
                float dx = (_radarPosition.X - pos.X);
                float dy =  (_radarPosition.Y - pos.Y );
                float  x = (float)(dx*Math.Cos(_rotation) + dy*Math.Sin(_rotation));
                float y= (float)(dy*Math.Cos(_rotation) - dx*Math.Sin(_rotation));
                x = x+_radius;
                y = y + _radius;
                x = (x * (screenRect.Width / 2)) / _radius - _objectTexture.Width/2;
                y = (y * (screenRect.Height / 2)) / _radius - _objectTexture.Height / 2;
                spriteBatch.Draw(_objectTexture, new Vector2(x,y), Color.White);
            }
        }
    }
}
